package com.bsg.upm.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.bsg.upm.dto.ArchDto;
import com.bsg.upm.entity.DefinitionSubServArchEntity;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;

/**
 * 架构管理业务处理类
 * 
 * @author HCK
 *
 */
@Service
public class ArchService extends BaseService {

	/**
	 * 获取架构列表
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return 操作结果
	 * @throws APIException
	 *             API异常处理类
	 */
	public RespJson list(Map<String, Object> paramMap) throws APIException {
		try {
			JSONArray retJsonArray = new JSONArray();
			List<DefinitionSubServArchEntity> archs = definitionSubServArchDao.list(paramMap);
			for (DefinitionSubServArchEntity arch : archs) {
				ArchDto archDto = buildShowDto(arch, false);
				retJsonArray.add(archDto);
			}
			return RespJsonFactory.buildOK(retJsonArray);
		} catch (Exception e) {
			logger.error("架构查询异常", e);
			throw new APIException("架构查询异常:" + e.getMessage());
		}
	}

	private ArchDto buildShowDto(DefinitionSubServArchEntity arch, boolean showDetail) {
		ArchDto archDto = new ArchDto();
		archDto.setId(arch.getId());
		archDto.setName(arch.getName());
		archDto.setCode(arch.getCode());
		archDto.setType(arch.getType());
		archDto.setUnitNum(arch.getUnitNum());
		archDto.setSequence(arch.getSequence());
		archDto.setHighAvailability(arch.getHighAvailability());
		archDto.setConsistency(arch.getConsistency());
		archDto.setCapability(arch.getCapability());
		archDto.setDefaults(arch.getDefaults());
		archDto.setHasStandBy(arch.getHasStandBy());
		return archDto;
	}
}
